# -*- coding: utf-8 -*-
# &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
# DTCloud v2.0
# QQ:35350428
# 邮件:35350428@qq.com
# 手机：13584935775
# 作者：'Amos'
# 公司网址： www.dtcloud360.com
# Copyright 中亿丰数字科技有限公司 2012-2024 Amos
# 日期：2024-01-30
# &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

import logging
from datetime import datetime, timedelta
_logger = logging.getLogger(__name__)
from functools import partial
from itertools import groupby
from dtcloud import api, fields, models, SUPERUSER_ID, _
from dtcloud.exceptions import AccessError, UserError, ValidationError
from dtcloud.tools.misc import formatLang, get_lang
from dtcloud.osv import expression
from dtcloud.tools import float_is_zero, float_compare

ORDER_STATES = [
    ('新建', '新建'),
    ('处理中', '处理中'),
    ('已发布', '已发布'),
]

READONLY_STATES = {
    '已发布': [('readonly', True)],
    '处理中': [('readonly', True)],
}

class bt_users(models.Model):
    _name = 'bt.users'
    _description = '智慧工地-员工'
    _order = 'id DESC'
    #_inherit = ['mail.thread', 'mail.activity.mixin']  # 引入社交：可以记录附件，保存历史字段变更信息
    _check_company_auto = True

    # 常用字段
    name = fields.Char(string='名称', required=True, default=lambda self: 'New', states=READONLY_STATES)
    color = fields.Integer(string='颜色', default=1)
    active = fields.Boolean(default=True, string='是否归档')
    date = fields.Date('发布日期', states=READONLY_STATES,
                       default=lambda self: fields.Date.context_today(self),
                       copy=False, help=u"创建日期")
    sequence = fields.Integer(string='排序', default=50)
    user_id = fields.Many2one('res.users', string='用户', index=True, default=lambda self: self.env.user)
    company_id = fields.Many2one('res.company', string='公司',  default=lambda s: s.env.company.id,index=True)
    year = fields.Integer(string='年度', required=True, index=True, default=datetime.now().year)
    month = fields.Integer(string='月度', required=True, index=True, default=datetime.now().month)
    week = fields.Integer(string='周', required=True, default=fields.Datetime.now().isocalendar()[1])
    day = fields.Integer(string='天', required=True, default=fields.Datetime.now().day)
    week_day = fields.Integer(string='星期几', required=True, default=fields.Datetime.now().isoweekday())
    hours = fields.Integer(string='时', required=True, default=fields.Datetime.now().hour)
    state = fields.Selection(ORDER_STATES, string='状态', readonly=True, copy=False, index=True,default='新建')

    @api.model
    def create(self, vals):
        if vals.get('name', 'New') == 'New':
            vals['name'] = self.env['ir.sequence'].next_by_code('bt.users') or '/'
        result = super(bt_users, self).create(vals)
        return result

    def write(self, vals):
        return super(bt_users, self).write(vals)


    def unlink(self):
        for order in self:
            if order.state != '新建':
                raise UserError('%s:只能删除新建单据!' % order._description)
        return super(bt_users, self).unlink()

    def button_open_url(self):
        url = 'data_company'
        return {
            'type': 'ir.actions.act_url',
            'url': url,
        }

    def button_draft(self):
        values = {
            'state': '新建',
        }
        self.write(values)

    def button_loading(self):
        self.state = '处理中'

    def button_done(self):
        self.state = '已发布'
